/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.controller;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.JsonResponse;
import edu.upc.clase.demo.entity.Sala;
import edu.upc.clase.demo.service.SalaService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
/**
 *
 * @author usuario
 */
@Controller("salaController")
public class DetalleReservSalaServAdicController {
    @Autowired
    private SalaService salaService;
    private static Logger log = LoggerFactory.getLogger(SalaController.class);
 
    @RequestMapping("/sala/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("sala/index");
        List<Sala> sala = salaService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("sala", sala);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
     @RequestMapping(value = "/sala/new", method = RequestMethod.GET)
    public ModelAndView newSala() {
        ModelAndView mav = new ModelAndView("sala/new");
        Sala sala = new Sala();
        mav.getModelMap().put("sala", sala);
        return mav;
    }
    @RequestMapping(value = "/sala/edit", method = RequestMethod.GET)
    public ModelAndView editSala(@RequestParam("id")String id) {    
        ModelAndView mav = new ModelAndView("sala/edit");
        Sala sala = salaService.buscar(id);
        mav.getModelMap().put("sala", sala);
        return mav;        
    }
    
    @RequestMapping(value="/sala/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("sala") Sala sala, SessionStatus status) {
        salaService.actualizar(sala);
        status.setComplete();
        return "redirect:/pages/sala/index";
    }
    
    @RequestMapping("/sala/delete")
    public ModelAndView delete(@RequestParam("id")String id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/sala/index");
        Sala sala = salaService.buscar(id);
        salaService.eliminar(sala);
        return mav;
    }
    
    @RequestMapping(value = "/sala/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("resultado/resultadoBusqueda");
        log.info("resultado = " + salaService.buscar(criterio.getNombre()));
        mav.getModel().put("resultado",salaService.buscar(criterio.getNombre()));
        return mav;
    }
    
    @RequestMapping(value = "/listarSalas", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody JsonResponse listMyFiles() {
        List<Sala> sala = salaService.buscarTodos();
        return new JsonResponse(true, sala);
    }

}
